package cn.com.ejiakanghu.util;

import cn.com.ejiakanghu.constant.PrintConstant;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.db.vo.anno.OrderContentDto;
import org.linlinjava.litemall.db.vo.anno.OrderContentGoodsDto;

import java.util.List;

/**
 * @author wj
 * @date 2021/10/22 13:56
 */
public class TemplateUtil {

    private static final String QRCODE="https://qrc.ejiakanghu.cn/qrcode/order?code=";
    private static final String BACK_QRCODE="https://qrc.ejiakanghu.cn/qrcode/back-order?code=";

    private static String ifnull(String s) {
        return StringUtils.isEmpty(s) ? "" : s;
    }

    private static String divider (){
        return "--------------------------------<BR>";
    }

    private static String header(OrderContentDto orderContentDto){
        StringBuilder content = new StringBuilder();
        content.append("<C>围老</C><BR>");
        content.append("<BR>");
        content.append(ifnull(orderContentDto.getStoreName()) + "<BR>");
        content.append("客服电话：" + ifnull(orderContentDto.getStorePhone()) + "<BR>");
        if (!PrintConstant.PRINT_ORDER_SELF_TYPE.equals(orderContentDto.getOrderType())) {
            content.append("配货员：" + ifnull(orderContentDto.getPicker()) + "<BR>");
        }
        content.append("客户：" + ifnull(orderContentDto.getUserName()) + " " + ifnull(orderContentDto.getUserPhone()) + "<BR>");
        if (PrintConstant.PRINT_ORDER_DELIVERY_TYPE.equals(orderContentDto.getOrderType())) {

            content.append("收货地址：" + ifnull(orderContentDto.getUserAddress()) + "<BR>");
            if(orderContentDto.getIsThirdPart()!=null&&orderContentDto.getIsThirdPart()==1){
                content.append("物流：" + ifnull(orderContentDto.getLogisticsName()) + " " + ifnull(orderContentDto.getLogisticsPhone()) + "<BR>");
            }
        }
        if (PrintConstant.PRINT_ORDER_SELF_TYPE.equals(orderContentDto.getOrderType())) {
            if(StringUtils.isNotBlank(orderContentDto.getSelfCode())){
                content.append("<BR>");
                content.append("自提取货码："+orderContentDto.getSelfCode());
                content.append("<BR>");
            }
        }
        content.append(divider());
        content.append("<B>" + ifnull(orderContentDto.getOrderType()) + "</B><BR>");
        content.append("<BR>");
        if (!PrintConstant.PRINT_ORDER_DELIVERY_TYPE.equals(orderContentDto.getOrderType())) {
            content.append(orderCode(orderContentDto));
        }
        return content.toString();
    }

    private static String orderCode(OrderContentDto orderContentDto){
        StringBuilder content = new StringBuilder();
        content.append("订单号：" + ifnull(orderContentDto.getOrderSn()) + "<BR>");
        content.append("支付流水号：" + ifnull(orderContentDto.getPayNo()) + "<BR>");
        content.append("<BR>");
        return content.toString();
    }

    private static String goods(List<OrderContentGoodsDto> orderContentDto){
        StringBuilder content = new StringBuilder();
        if (CollectionUtils.isNotEmpty(orderContentDto)) {
            for (OrderContentGoodsDto goods : orderContentDto) {
                if(StringUtils.isNotBlank(goods.getUrgentTime())){
                    content.append("<BOLD>"+"加急订单"+"</BOLD><BR>");
                    content.append("加急时间:"+goods.getUrgentTime());
                }
                if(StringUtils.isNotBlank(goods.getSubscribeTime())){
                    content.append("<BOLD>"+"预约订单"+"</BOLD><BR>");
                    content.append("预约时间:"+goods.getSubscribeTime());
                }
                if(StringUtils.isNotBlank(goods.getHospitalHourTime())){
                    content.append("<BOLD>"+"医院小时达"+"</BOLD><BR>");
                    content.append("到达时间:"+goods.getHospitalHourTime());
                }
                if(StringUtils.isNotBlank(goods.getHospitalHourSubTime())){
                    content.append("<BOLD>"+"医院预约小时达"+"</BOLD><BR>");
                    content.append("到达时间:"+goods.getHospitalHourSubTime());
                }
                content.append("<BOLD>"+ifnull(goods.getGoodsName())+"</BOLD>"+ (goods.getDeposit() ? " 押金￥" : " ￥") + ifnull(goods.getGoodsAmount()) + " X" + ifnull(goods.getGoodsNumber()) + "<BR>");
                content.append("规格：" + ifnull(goods.getSpec()) + "<BR>");
                if(StringUtils.isNotBlank(goods.getRfid())){
                    content.append("设备：" + ifnull(goods.getRfid()) + "<BR>");
                }
                content.append("<BR>");
                content.append(divider());
            }
        }
        return content.toString();
    }


    private  static String bottom(OrderContentDto orderContentDto){
        StringBuilder content = new StringBuilder();
        if(PrintConstant.PRINT_ORDER_DELIVERY_TYPE.equals(orderContentDto.getOrderType())){
            content.append(orderCode(orderContentDto));
        }
        content.append(divider());
        content.append("--------------------------------<BR>");
        content.append("<C>订单二维码</C><BR>");
        content.append("<BR>");
        content.append("<QR>" + QRCODE+"LBB-"+ifnull(orderContentDto.getOrderUrl()) + "</QR><BR>");
        content.append("<C>订单条形码</C><BR>");
        content.append("<C><BC128_A>" +"LBBBAR"+ ifnull(orderContentDto.getOrderUrl()) + "</BC128_A></C><BR>");

        return content.toString();
    }
    /**
     * 打印模板
     * @return
     */
    public static String printTemplate(OrderContentDto orderContentDto){
        StringBuilder content = new StringBuilder();
        content.append(header(orderContentDto));
        content.append(goods(orderContentDto.getGoodsList()));
        content.append(divider());
        content.append(bottom(orderContentDto));
        return content.toString();
    }


}
